package com.kingre.et.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.context.SecurityContextHolder;

import com.kingre.et.model.School;
import com.kingre.et.model.User;
import com.kingre.et.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;

public class AccountsManagerAction extends ActionSupport {
    private static final long serialVersionUID = -373364582885050819L;
    private static final Logger logger = Logger.getLogger(AccountsManagerAction.class);
    private String roleName;
    @Autowired
    private IUserService userService;
    private List<User> accountsList = new ArrayList<User>();
    private List<School> schoolList = new ArrayList<School>();

    private User user;
    private Integer id;
    private Integer schoolId;
    private String newPassword;

    @Override
    public String execute() {
        roleName = SecurityContextHolder.getContext().getAuthentication().getAuthorities()[0].getAuthority();
        schoolList = userService.findSchoolAll();
        accountsList = userService.findAccounts();
        return SUCCESS;
    }

    public String delete() {
        logger.debug("id =" + id);
        userService.delete(id);
        return execute();
    }

    public String add() {
        logger.debug("schoolId =" + schoolId);
        if (userService.usernameExist(user.getUsername())) {
            addFieldError("dd", getText("user.username.exist"));
            return execute();
        }
        userService.add(user, schoolId);
        addFieldError("dd", getText("user.add.ok"));
        return execute();
    }

    public String update() {
        roleName = SecurityContextHolder.getContext().getAuthentication().getAuthorities()[0].getAuthority();
        User currentUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        userService.updatePasswrod(currentUser, newPassword);
        return SUCCESS;
    }

    public List<School> getSchoolList() {
        return schoolList;
    }

    public void setSchoolList(List<School> schoolList) {
        this.schoolList = schoolList;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public List<User> getAccountsList() {
        return accountsList;
    }

    public void setAccountsList(List<User> accountsList) {
        this.accountsList = accountsList;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Integer getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(Integer schoolId) {
        this.schoolId = schoolId;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
}
